<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-navbar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 个人信息 -->
    <input
      type="file"
      hidden
      ref='file'
      accept="image/*"
      @change='onChange'
    >
    <van-cell
      class="avatar-cell"
      title="头像"
      is-link
      center
      @click="$refs.file.click()"
    >
      <van-image
        class="avatar"
        round
        fit="cover"
        :src="userProfile.photo"
      />
    </van-cell>
    <van-cell
      title="昵称"
      :value="userProfile.name"
      is-link
      @click='show=true'
    />
    <van-cell
      title="性别"
      :value="userProfile.gender?'女':'男'"
      is-link
      @click='isEditGendershow=true'
    />
    <van-cell
      title="生日"
      :value="userProfile.birthday"
      is-link
      @click='isEditBirthdayshow=true'
    />
    <!-- /个人信息 -->

    <van-popup
      v-model="show"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <Updatename
        :username='userProfile.name'
        @close='show=false'
        @confirm='confirm'
      ></Updatename>
    </van-popup>

    <!-- 修改性别 -->
    <van-popup
      v-model="isEditGendershow"
      position="bottom"
    >
      <Update-gender
        v-model='userProfile.gender'
        @close='isEditGendershow=false'
        @confirm='genderConfirm'
      ></Update-gender>
    </van-popup>

    <!-- 修改生日 -->
    <van-popup
      v-model="isEditBirthdayshow"
      position="bottom"
    >
      <Update-birthday
        v-model='userProfile.birthday'
        @close='isEditBirthdayshow=false'
        @confirm='birthdayConfirm'
      ></Update-birthday>
    </van-popup>
    <!-- 修改用户头像 -->
    <van-popup
      v-model="isEditPhotoshow"
      class="updata-photo-popup"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo
        v-if='isEditPhotoshow'
        :previewimage='previewimage'
        @close='isEditPhotoshow=false'
        @confirm='getphoto'
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import { confirm } from '@/utils/confirm'
import Updatename from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo.vue'

export default {
  name: 'UserProfile',
  components: {
    Updatename,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data() {
    return {
      userProfile: {},
      show: false,
      isEditGendershow: false,
      isEditBirthdayshow: false,
      isEditPhotoshow: false,
      previewimage: null
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getUserProfile()
  },
  mounted() { },
  methods: {
    async getUserProfile() {
      const res = await getUserProfile()
      this.userProfile = res.data.data
    },
    async confirm(value) {
      // this.$toast.loading({
      //   message: '保存中',
      //   forbidclick: true
      // })
      // try {
      //   await updataUserProfile({ name: arg[1] })
      //   this.$toast.success("保存成功")
      //   this.userProfile.name = arg[1]
      //   this.show = false
      // } catch (error) {
      //   if (error && error.response.status === 409) {
      //     this.$toast.fail("该昵称已存在")
      //   }
      // }
      await confirm('name', value, this.show)
      this.userProfile.name = value
    },
    async genderConfirm(index) {
      await confirm('gender', index, this.isEditGendershow)
      this.userProfile.gender = index
    },
    async birthdayConfirm(birthday) {
      await confirm('birthday', birthday, this.isEditBirthdayshow)
      this.userProfile.birthday = birthday
    },
    onChange() {
      // 展示弹出层
      this.isEditPhotoshow = true
      // 在弹出层里面预览图片
      // const blob = URL.createObjectURL(this.$refs.file.files[0])
      this.previewimage = this.$refs.file.files[0]
      this.$refs.file.value = ''
    },
    getphoto(value) {
      console.log(value)
      this.userProfile.photo = value
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 40px;
      height: 40px;
    }
  }
  .van-popup {
    background-color: #f5f7f9;
  }
  .updata-photo-popup {
    background-color: #000;
  }
}
</style>